﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data.Sql;

namespace RestaurantManagerWebApp.Pages.StockManagement.New
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        string connection = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnAddSupplier_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(connection);
            conn.Open();

            string sname = txtSupplierName.Text;
            string snum = txtSupplierNo.Text;
            string slocation = txtLocation.Text;
            string semail = txtEmail.Text;
            string ingredient = txtIngredient.Text;

            AddSupplier(conn, sname, snum, slocation, semail, ingredient);
        }

        private void AddSupplier (SqlConnection conn, string name, string num, string location, string email, string ingredient)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;

                // *Remember to change localhost to respective SQL DB login in web.config file (vary from pc to pc)
                // Re-visit to check on the incremental value for s_id
                // Currently on static input basis

                cmd.CommandText = "INSERT INTO SUPPLIER (s_id,s_name, s_number, s_loc, s_email, i_name) VALUES (2,@sname, @snum, @sloc, @semail, @ingredient)";

                cmd.Parameters.AddWithValue("@sname", name);
                cmd.Parameters.AddWithValue("@snum", num);
                cmd.Parameters.AddWithValue("@sloc", location);
                cmd.Parameters.AddWithValue("@semail", email);
                cmd.Parameters.AddWithValue("@ingredient", ingredient);

                cmd.ExecuteNonQuery();
                
            }
            catch (SqlException ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }
    }
}